package com.yunfei.api.v2.service;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.param.LongTokenParam;
import com.yunfei.api.v2.param.QueryUserNoParam;
import com.yunfei.api.v2.result.queryUserNo.QueryUserNoResultJson;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.utils.http.CreatePersonInfoHttpUtil;
import com.yunfei.utils.string.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @Description:查询用户编号
* @Author: zhengyunfei
* @Date: 2019/8/9
* @time:10:48
*/
@Service
public class QueryUserNoService {
    @Autowired
    private Environment env;
    @Autowired
    private GetLongTokenService getLongTokenService;
    /**
    * @Description: 根据证件号码查询signerId
     * @param  token 可选参数，如果没有，则调用长效令牌
    * @Author: zhengyunfei
    * @Date: 2019/8/15
    * @time:16:26
    */
    public String queryUserNo(String certifyNum,String token){
        ResultModel resultModel=new ResultModel();
        String signerId="";//用户编号
        try{
                LongTokenParam longTokenParam=new LongTokenParam();
                if(StringUtil.isNullOrEmpty(token)){
                     token=getLongTokenService.getLongToken(longTokenParam);
                }
                QueryUserNoParam queryUserNoParam=new QueryUserNoParam();
                List<String> signerIdList=new ArrayList<>();
                signerIdList.add(certifyNum);
                queryUserNoParam.setCertifyNumList(signerIdList);
                JSONObject queryUserInfoJson=JSONObject.fromObject(queryUserNoParam);
                String queryUserInfoJsonParam=queryUserInfoJson.toString();
                JSONObject queryJson=CreatePersonInfoHttpUtil.post(token, YunHeTongApiContant.QUERY_USER_INFO_API,queryUserInfoJsonParam);
                QueryUserNoResultJson queryUserInfoResultJson=(QueryUserNoResultJson) JSONObject.toBean(queryJson,QueryUserNoResultJson.class);
                if(queryUserInfoResultJson.getCode()==200){
                    signerId=queryUserInfoResultJson.getData().get(0).toString().split("=")[1].replace("}","").replace("]","").replace("\r\n","");//证件号
                }
        }catch (Exception e){
            e.printStackTrace();
        }
        return signerId;
    }

}
